/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package NicholasValueBeans;

import NicholasDatabase.SQLqueries;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ChuckToh
 */
public class Votes {

    private int votingListId, userId;
    private String topicVoted;
    private Date dateCreated;

    public Votes() {
    }

    public Votes(int votingListId, int userId, String topicVoted, Date dateCreated) {
        this.votingListId = votingListId;
        this.userId = userId;
        this.topicVoted = topicVoted;
        this.dateCreated = dateCreated;
    }

    public void setVotingListId(int votingListId) {
        this.votingListId = votingListId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public void setTopicVoted(String topicVoted) {
        this.topicVoted = topicVoted;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public boolean isRepeatedVote() {
        boolean repeated = false;
        SQLqueries sql = new SQLqueries();
        try {
            ResultSet voteQuery = sql.voteQuery(votingListId, userId);
            if (voteQuery.next()) {
                repeated = true;
            }
            voteQuery.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
            repeated = true;
        }
        sql.closeQuery();
        return repeated;
    }

    public boolean insertVote() {
        boolean success = false;
        SQLqueries sql = new SQLqueries();
        try {
            int insertResults = sql.insertVote(votingListId, userId, topicVoted, dateCreated);
            if (insertResults != 0) {
                success = true;
            }
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        sql.closeQuery();
        return success;
    }

    public ResultSet getVotesByVotingListId(int votingListId) {
        SQLqueries sql = new SQLqueries();
        ResultSet votesByVotingListId = null;
        try {
            votesByVotingListId = sql.votesByVotingListIdQuery(votingListId);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return votesByVotingListId;
    }
}
